package com.gpf.marketing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gpf.marketing.mapper.AdvManageMapper;
import com.gpf.marketing.pojo.entity.AdvManage;
import com.gpf.marketing.pojo.entity.AdvPosition;
import com.gpf.marketing.pojo.vo.AdvMaVo;
import com.gpf.marketing.pojo.vo.AdvPoVo;
import com.gpf.marketing.service.AdvManageService;
import com.seventh.common.pojo.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-fifth
 * @description: 广告管理业务层
 * @author: 龚鹏飞
 * @create: 2021-07-21 18:29
 **/
@Service
public class AdvManageServiceImpl implements AdvManageService {
    @Resource
    private AdvManageMapper advManageMapper;


//    @Override
//    public R selectAdvManage( Integer currentPage,Integer pageSize, AdvMaVo advMaVo) {
//        Page<AdvManage> page = new Page<>(currentPage,pageSize);
//        //创建条件查询querywrapper对象
//        QueryWrapper<AdvManage> wrapper = new QueryWrapper<>();
//        //添加模块查询条件语句
//        if (StringUtils.isNotEmpty(advMaVo.getName())){
//            wrapper.like("name",advMaVo.getName());
//        }
//        advManageMapper.selectPage(page,wrapper);
//        System.out.println(page.getRecords());
//       return R.ok().put("records",page.getRecords()).put("total",page.getTotal());
//    }
    //关联查询
    @Override
    public R findName(Integer currentPage, Integer pageSize, AdvMaVo advMaVo) {
        Page<AdvManage> page = new Page<>(currentPage, pageSize);
        //创建条件查询querywrapper对象
        QueryWrapper<AdvManage> wrapper = new QueryWrapper<>();
        //添加模块查询条件语句
        if (StringUtils.isNotEmpty(advMaVo.getAname())){
            wrapper.like("aname",advMaVo.getAname());
        }
        IPage<AdvManage> page1 = advManageMapper.findName(page, wrapper);
        return R.ok().put("records", page1.getRecords()).put("total", page1.getTotal());
    }

    @Override
    public R getAdvList() {
         List<AdvManage> advManages = advManageMapper.selectList(null);
        return R.ok().put("adlist",advManages);
    }

    @Override
    public R insertAdvManage(AdvManage advManage) {
        int i=advManageMapper.insert(advManage);
       return (i>0)? R.ok(): R.error();
    }

    @Override
    public R updateAdvManage(AdvManage advManage) {
        int i=advManageMapper.updateById(advManage);
        return (i>0)? R.ok(): R.error();
    }

    @Override
    public R deleteById(Integer id) {
        int i=advManageMapper.deleteById(id);
        return (i>0)? R.ok(): R.error();
    }

    @Override
    public R selectById(Integer id) {
        System.out.println(id);
       AdvManage advManage=advManageMapper.selectById(id);
       return R.ok().put("advMange",advManage).put("massage","更新成功");
    }
    //名称唯一性校验根据
    @Override
    public R selectOneName(AdvManage advManage) {
        List list=advManageMapper.selectOneName(advManage);
        return (list!=null&&list.size()>0)? R.error().message("名称已存在"):R.ok().message("名称可以使用");
    }
//更改状态
    @Override
    public R upStatus(Integer id, Integer status) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        advManageMapper.update(null,wrapper);
        return R.ok();
    }


}
